package seGobangClient.gaming;

import java.util.Timer;
import java.util.TimerTask;
import javax.vecmath.Point2i;
import seGobangClient.ISceneController;
import seGobangClient.Rule;
import WebServiceModel.GamingStateModel;
import WebServiceGamingClient.GamingClient;
import java.awt.*;
/**
 *
 * @author plus
 */
public class OnlineLocalPlayer implements IPlayer{
    private static ISceneController MyController;
    private String Name;
    private int iTurns, iRegrets;
    private int iTime;                                                          //剩余时间，单位s
    private int RoomID, DeskID;
    private byte chess_flag;
    private Timer timerUpdateInfo;
    private Rule MyRule;
    private GamingClient msg=new GamingClient();

    public OnlineLocalPlayer(String Name,byte chessflag,Rule rule, int RoomID, int DeskID) {
        this.chess_flag=chessflag;
        this.Name = Name;
        MyRule=rule;
        MyRule.RegretTimes=0;
        this.RoomID=RoomID;
        this.DeskID=DeskID;
        resetPlayer();
    }
    
    public void resetPlayer(){
        iTurns=0;
        iRegrets=MyRule.RegretTimes;
        iTime=MyRule.TimeLimit;
        if (timerUpdateInfo!=null) timerUpdateInfo.cancel();
    }
    
    public void setController(ISceneController Controller){
        MyController=Controller;
    }
    
    public int getLastRegrets() {
        return iRegrets;
    }

    public int getLastTime() {
        return iTime;
    }

    public String getPlayerName() {
        return Name;
    }

    public int getTurns() {
        return iTurns;
    }

    public void gameResult(int iResult) {
       if (timerUpdateInfo != null) timerUpdateInfo.cancel();
       
       GamingStateModel gaming=GamingStateModel.getInstance();
       gaming.setDeskID(RoomID, DeskID);
       gaming.setPos(0, 0);
       gaming.setTime(getChessFlag());                               
       
        switch (iResult){
            case RESULT_WIN:
                gaming.setState(WSCommand.ACTION_WIN);    
                //WSSendCommand(new WSCommand(WSCommand.ACTION_WIN,Name,RoomID,DeskID,null));
                break;
            case RESULT_OPP_FORBIDDEN:
                gaming.setState(WSCommand.ACTION_WIN);
                //WSSendCommand(new WSCommand(WSCommand.ACTION_WIN,Name,RoomID,DeskID,null));
                break;
            case RESULT_DRAW:
                gaming.setState(WSCommand.ACTION_DRAW);
                //WSSendCommand(new WSCommand(WSCommand.ACTION_DRAW,Name,RoomID,DeskID,null));
                break;
        }        
         msg.sendMessage(gaming);  
    }
    
    public void turnToMe() {   
        timerUpdateInfo=new Timer();
        timerUpdateInfo.schedule(new TimerTask(){
            public void run(){
                MyController.doEvent("UpdatePlayerInfo", this);
                scanOnlineCommands();
                if (iTime==0){
                    
                    GamingStateModel gaming=GamingStateModel.getInstance();
                    gaming.setDeskID(RoomID, DeskID);
                    gaming.setPos(0, 0);
                    gaming.setState(WSCommand.ACTION_TIMEOUT);         
                    gaming.setTime(getChessFlag());
                    msg.sendMessage(gaming);                    
                    //WSSendCommand(new WSCommand(WSCommand.ACTION_TIMEOUT,this.chess_flag,RoomID,DeskID,null));
                    MyController.doEvent("SomeoneTimeout", this);
                    timerUpdateInfo.cancel();
                }
                else 
                    if (iTime>0) iTime--;
            }
        }, 0, GamingController.RefreshRate
        );
        MyController.doEvent("MarkCurrentPlayer", this);
        MyController.doEvent("EnableLocalInput",null);
    }

    private void scanOnlineCommands(){
        WSCommand currentStateCommand=null, currentPutCommand=null;
        GamingStateModel gaming=msg.getMessage(RoomID, DeskID);
        Point pos=gaming.getPos();
        currentStateCommand=new WSCommand(gaming.getState(),"",gaming.getTime(),gaming.getRoomID(),gaming.getDeskID(),new Point2i(pos.x,pos.y));
        
        //currentStateCommand=WSGetState(RoomID,DeskID,Name);
        //currentPutCommand=WSGetPut(RoomID,DeskID,Name);
        
        if(currentStateCommand!=null)
            if (this.chess_flag!=currentStateCommand.ChessID){                    //只处理对方信息
                switch (currentStateCommand.ActionID){
                    case WSCommand.ACTION_SURRENDER:
                        MyController.doEvent("RaiseSurrender", null);
                        break;
                    case WSCommand.ACTION_TIMEOUT:
                        MyController.doEvent("SomeoneTimeout", null);
                        break;
                    case WSCommand.ACTION_LINECUT:
                        MyController.doEvent("SomeoneLineCut", null);
                        break;
            }
        }
        
    }
    
    public void turnOver(boolean isRegret) {
        if (timerUpdateInfo!=null) timerUpdateInfo.cancel();
       // timerUpdateInfo=null;
        
        if(isRegret)
           this.iTurns--; 
        else
            this.iTurns++;
        
    }

    public void putChess(Point2i pos) {
        //WSSendCommand(new WSCommand(WSCommand.ACTION_PUTCHESS,Name,RoomID,DeskID,pos));
        GamingStateModel gaming=GamingStateModel.getInstance();
        gaming.setDeskID(RoomID, DeskID);
        gaming.setPos(pos.x, pos.y);
        gaming.setState(WSCommand.ACTION_PUTCHESS);         
        gaming.setTime(this.chess_flag);
        msg.sendMessage(gaming);
        
        
        if (this.chess_flag==-1)
            MyController.doEvent("BlackChessPut", pos);
        else
            MyController.doEvent("WhiteChessPut", pos);
    }

    public void regret() {//网络模式下不允许悔棋
    }

    public void surrender() {
        //WSSendCommand(new WSCommand(WSCommand.ACTION_SURRENDER,Name,RoomID,DeskID,null));
        GamingStateModel gaming=GamingStateModel.getInstance();
        gaming.setDeskID(RoomID, DeskID);
        gaming.setPos(0, 0);
        gaming.setState(WSCommand.ACTION_SURRENDER);       
        gaming.setTime(this.chess_flag);
        msg.sendMessage(gaming);
        
        MyController.doEvent("RaiseSurrender", this);
    }
    
    public byte getChessFlag(){
        return this.chess_flag;
    }

}
